package views.button
{
	import flash.display.FrameLabel;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	public class NormalButtonView extends MovieClip
	{
		private var buttonStateList:Object;
		private var _buttonState:String;
		
		private var _selected:Boolean = false;
		private var _enabled:Boolean = true;
		
		private var target:Sprite;
		
		public function NormalButtonView()
		{
			initialize();
		}
		
		private function initialize():void
		{
			stop();

			buttonStateList = new Object;
			var labels:Array = currentLabels;
			for each(var label:FrameLabel in labels)
			{
				var startIndex:int = label.frame;
				var endIndex:int = totalFrames;
				for each(var nextLabel:FrameLabel in labels)
				{
					if(label == nextLabel) continue;	
					
					var gap:int = nextLabel.frame - startIndex - 1 ;
					endIndex = gap >= 0 && gap < endIndex - startIndex ? nextLabel.frame - 1 : endIndex;
				}
				
				addFrameScript(endIndex - 1,function():void{stop();});
				var state:ButtonState = new ButtonState(label.name,startIndex,endIndex);
				buttonStateList[state.state] = state;
			}
			
			target = getChildByName("buttonArea") as Sprite;
			if(target != null)
			{
				for(var i:int = 0;i < numChildren;i++)
				{
					var subview:Sprite = this.getChildAt(i) as Sprite;
					if(subview == null || subview == target)
						continue;
					subview.mouseEnabled = false;
				}
			}else{
				target = this;
			}
			target.buttonMode = true;
			target.addEventListener(MouseEvent.MOUSE_OVER,function(evt:Event):void
														 {
															if(selected || !enabled)
															 	return;
														  	buttonState = ButtonState.BUTTON_OVER
														});
			target.addEventListener(MouseEvent.MOUSE_OUT,function(evt:Event):void
														{
															if(selected || !enabled)
																return;
															buttonState = ButtonState.BUTTON_OUT
														});
			target.addEventListener(MouseEvent.MOUSE_DOWN,function(evt:Event):void
														{
															if(selected || !enabled)
																return;
														 	buttonState = ButtonState.BUTTON_DOWN
														});
			target.addEventListener(MouseEvent.MOUSE_UP,function(evt:Event):void
														{
															if(selected || !enabled)
																return;
														 	buttonState = ButtonState.BUTTON_UP
														});
		}
		
		public function set buttonState(state:String):void
		{
			if(_buttonState == state)
				return;

			if(buttonStateList.hasOwnProperty(state))
			{
				_buttonState = state;
			}else{
				if(buttonStateList.hasOwnProperty(ButtonState.BUTTON_NORMAL))
					_buttonState = ButtonState.BUTTON_NORMAL;
				else
					return;
			} 
				
			var btnState:ButtonState = buttonStateList[_buttonState];
			gotoAndPlay(btnState.startFrame);
		}
		
		public function get buttonState():String
		{
			return _buttonState;
		}
		
		public function set selected(_selected:Boolean):void
		{
			if(this._selected == _selected)
				return;
			this._selected = _selected;

			if(selected)
				buttonState = ButtonState.BUTTON_SELECTED;
			else
				buttonState = ButtonState.BUTTON_UNSELECTED;
			mouseEnabled = enabled && !selected;
			mouseChildren = enabled && !selected;
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}
		
		public override function set enabled(_enabled:Boolean):void
		{
			if(this._enabled == _enabled)
				return;
			this._enabled = _enabled;
			
			if(enabled)
				buttonState = ButtonState.BUTTON_ENABLED;
			else
				buttonState = ButtonState.BUTTON_DISABLED;
			
			mouseEnabled = enabled && !selected;
			mouseChildren = enabled && !selected;
		}
		
		public override function get enabled():Boolean
		{
			return _enabled;
		}
		
	}
}